﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.OrderService;
using Newtonsoft.Json;
using System.Web.Services;
using RN.ERP.Web.ModelService;
namespace RN.ERP.Web.CA
{
    public partial class OrderInfoVerify : BasePage
    {
        OrderServiceClient client = new OrderServiceClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hidAudit.Value = Request.QueryString["audit"];
                if (!string.IsNullOrEmpty(hidAudit.Value))
                {
                    lbtnConfirm.Visible = false;
                    lbtnAudit.Visible = true;
                }
                hidGuid.Value = Request.QueryString["guid"];//订单guid
                ModelService.CaPayInfo caPayInfo = JsonConvert.DeserializeObject<ModelService.CaPayInfo>(client.GetOrderInfo(hidGuid.Value));//获取订单信息
                if (caPayInfo != null)
                {
                    lbCode.Text = caPayInfo.Code;
                    txtTitle.Text = caPayInfo.iTitle;
                    txtLinkMan.Text = caPayInfo.LinkPerson;
                    txtTel.Text = caPayInfo.officeTel;
                    txtMobile.Text = caPayInfo.mobileTel;
                    txtPostCode.Text = caPayInfo.postcode;
                    txtAddress.Text = caPayInfo.SendAddress;
                    txtNotice.Text = caPayInfo.Notice;
                    lbSum.Text = caPayInfo.SumPay.ToString();

                    txtTitle.Enabled = false;
                    txtLinkMan.Enabled = false;
                    txtTel.Enabled = false;
                    txtMobile.Enabled = false;
                    txtPostCode.Enabled = false;
                    txtAddress.Enabled = false;
                    txtNotice.Enabled = false;
                    BindData();
                }
            }
        }

        private void BindData()
        {
            Guid iGuid = Guid.Empty;
            if (Guid.TryParse(hidGuid.Value, out iGuid))
            {
                List<QueryInvoiceInfoCondition> queryList = new List<QueryInvoiceInfoCondition>();
                queryList.Add(new QueryInvoiceInfoCondition()
                {
                    KeyGUID = new Fly.Common.Linq.ParameterValue<Guid>() { V = iGuid, O = Fly.Common.Linq.OptEnum.Equal }
                });
                List<ModelService.CaPayRelation> result = JsonConvert.DeserializeObject<List<ModelService.CaPayRelation>>(client.GetInvoicesList(JsonConvert.SerializeObject(queryList), 0, 0));
                RadGrid1.DataSource = result;
                RadGrid1.DataBind();
            }
        }


        protected void lbtnConfirm_Click(object sender, EventArgs e)
        {
            OperationResult result = new OperationResult();
            List<Guid> guids = new List<Guid>();
            guids.Add(Guid.Parse(hidGuid.Value));
            if (!string.IsNullOrEmpty(hidAudit.Value))
            {
                result = JsonConvert.DeserializeObject<ModelService.OperationResult>(client.OrderAudit(guids.ToArray(), Guid.Parse(UserInfo.CorpUserGuid)));
            }
            else
            {
                result = JsonConvert.DeserializeObject<ModelService.OperationResult>(client.VerifyOrder(guids.ToArray(), Guid.Parse(UserInfo.CorpUserGuid)));
            }
            if (result.Result)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('" + result.Message + "','',CloseWin);", true);
                //RadWindowManager1.RadAlert(result.Message, 200, 100, "提示", "CloseWin", "");
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('" + result.Message + "');", true);
                //RadWindowManager1.RadAlert(result.Message, 300, 150, "提示", "");
            }
        }
    }
}